#!/usr/bin/ruby

require 'mysql'
require '../db_setting'

ct = 0
weight = 1.0
before_asin = ""
before_layer = 0

db = Mysql::new(DB_HOST, DB_USER, DB_PASS, DB_NAME)
res = db.query("select asin, id, layer from link_book2browse_node as A, browse_nodes as B where A.browse_node_id = B.id order by A.asin, B.layer desc")
res.each do |row|
  asin = row[0]
  id = row[1]
  layer = row[2]

  weight *= 0.2 if layer != before_layer
  weight  = 1.0 if asin != before_asin

  sql = "update link_book2browse_node set weight = #{weight} where asin = '#{asin}' and browse_node_id = '#{id}'"
  db.query(sql)

  before_asin = asin
  before_layer = layer

  ct += 1
  if ct % 100 == 0 then
    puts "ct: #{ct}"
  end
end
